/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.admin.accounts.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.History;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.ClickListener;



import com.citep.web.gwt.module.Presentation;
import com.citep.web.admin.transactions.client.TransactionEntryModule;
import com.citep.web.admin.users.client.UserEntryModule;
import com.citep.web.admin.users.client.UserListModule;
import com.citep.web.admin.users.client.UserLoginModule;
import com.citep.web.gwt.dto.*;
import com.citep.web.admin.transactions.client.TransactionImportModule;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AccountsApp implements EntryPoint, HistoryListener {

	protected Presentation currPresentation;
	
  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    History.addHistoryListener(this);
    
    String initToken = History.getToken();
    
    if(initToken.length() == 0) {
    	initToken = HistoryEncoder.encode("user_login",null);
        History.newItem(initToken);
    } else {
        History.newItem(initToken);
      onHistoryChanged(initToken);
    	
    }
  }
  
  protected void clearPage() {
	  if(currPresentation != null) {
		RootPanel.get("context").clear();
	    RootPanel.get("main_content").clear();
	  }
  }
  
  protected void showCurrent() {
	  if(currPresentation != null) {
		  
		  //context is optional
		  if(currPresentation.getContext() != null)
			  RootPanel.get("context").add(currPresentation.getContext());
		  
		  RootPanel.get("main_content").add(currPresentation.getMainPanel());
	  }
  }
  
  protected void launchAccountEntry(Integer oid) {
	    AccountEntryModule ac = AccountEntryModule.getInstance();
	    
	    clearPage();	    
	    currPresentation = ac.getPresentation();
	    showCurrent();
	    ac.init(oid);	    
  }
  
  protected void launchAccountList(Integer oid) {
	  
	  AccountListModule al = AccountListModule.getInstance();

	  clearPage();
	  currPresentation = al.getPresentation();
	  showCurrent();	  
	  al.init(oid);
  }
  
  protected void launchTransactionEntry(Integer oid) {
	  TransactionEntryModule te = TransactionEntryModule.getInstance();
	  
	  clearPage();
	  currPresentation = te.getPresentation();
	  showCurrent();	  
	  te.init(oid);
  }
  
  protected void launchUserEntry(Integer oid) {
	  UserEntryModule ue = UserEntryModule.getInstance();
	  
	  clearPage();
	  currPresentation = ue.getPresentation();
	  showCurrent();
	  ue.init(oid);
  }

  protected void launchUserList(Integer oid) {
	  
	  UserListModule ul = UserListModule.getInstance();

	  clearPage();	 	  
	  currPresentation = ul.getPresentation();
	  showCurrent();
	  ul.init(null);
  }
  
  protected void launchUserLogin() {
	  UserLoginModule ul = UserLoginModule.getInstance();
	  clearPage();
	  currPresentation = ul.getPresentation();
	  showCurrent();
	  ul.init(null);
  }
  
  protected void launchTransactionsImport(Integer oid) {
	  TransactionImportModule ti = TransactionImportModule.getInstance();
	  clearPage();
	  currPresentation = ti.getPresentation();
	  showCurrent();
	  ti.init(oid);
  }
  
  
  public void onHistoryChanged(String token) {

	  Integer oid = HistoryEncoder.getObjectId(token);
	  String module = HistoryEncoder.getModule(token);
	  
	  if(module.equals("account_list"))
		  launchAccountList(oid);
	  else if(module.equals("account_entry"))		  
		  launchAccountEntry(oid);
	  else if(module.equals("edit_account"))
		  launchAccountEntry(oid);
	  else if(module.equals("transaction_entry"))
		  launchTransactionEntry(oid);
	  else if(module.equals("user_entry"))
		  launchUserEntry(oid);
	  else if(module.equals("edit_user"))
		  launchUserEntry(oid);
	  else if(module.equals("user_list"))
		  launchUserList(oid);
	  else if(module.equals("user_login"))
		  launchUserLogin();
	  else if(module.equals("transactions_import"))
		  launchTransactionsImport(oid);
		  
  }
}
